// 
function ShopItemIcon(region, name)
{
    if (this instanceof ShopItemIcon)
    {
        UI.Base.call(this);
        this.resize(region);
        this.enableRawEvent(UI.Base.MOUSE_CLICK);
        this.registerListener(UI.Base.MouseEvent.CLICK, bind(this.onClick, this));
        var textP = document.createElement('p');
        textP.style.marginLeft = 'auto';
        textP.style.marginRight = 'auto';
        textP.style.width = '90%';
        textP.appendChild(document.createTextNode("vegname = " + name));
        this.dom.appendChild(textP);
        this.enableTextTip(name + " click to open bug dialog");
        this.vegName = name;
    }
    else
    {
        return new ShopItemIcon(region, name);
    }
}
inheritPrototype(ShopItemIcon, UI.Base);


ShopItemIcon.prototype.onClick = function(e)
{
    log.info("click shopItemIcon, " + this.vegName);
    var dialog = new VegBuyDialog(this.vegName);
    dialog.doModal();
}

function Shop()
{
    if (this instanceof Shop)
    {
        UI.Dialog.call(this);
        this.setClassName("ui_shop");
        this.leftButtonName = "OK";
        this.content = null;
        this.renderBasic();
        Models.getShopItemList(bind(this.onReceiveShopItem, this));
    }
    else
    {
        return new Shop();
    }
}
inheritPrototype(Shop, UI.Dialog);

Shop.prototype.render = function()
{
    
}

Shop.prototype.renderBasic = function()
{
    this.dom.style.backgroundColor = 'white';
    this.resize(new Rect(0, 0, 440, 470));
    this.content = new UI.Base();
    this.content.dom.style.position = "relative";
    this.content.dom.style.marginLeft = "20px";
    this.content.dom.style.marginRight = "20px";
    this.content.dom.style.marginBottom = "10px";
    this.content.dom.style.marginTop = "20px";
    this.content.setSize(new Point(400, 400));
    this.content.dom.style.overflowY = 'scroll';
    this.content.dom.style.overflowX = 'hidden';
    this.content.dom.style.backgroundColor = 'blue';
    this.appendChild(this.content);
    this.renderDefaultButtons();
}

Shop.prototype.getSize = function()
{
    return new Point(440, 470);
}

Shop.prototype.onReceiveShopItem = function(items)
{
    log.info("shop itemp received!");
    for (var i = 0; i < items.length; i++)
    {
        var row = Math.floor(i / 4);
        var col = i % 4;
        this.content.appendChild(new ShopItemIcon(new Rect(col * 100, row * 100, 100, 100), items[i]));
    }
}

function VegInfoView(veg, buy)
{
    if (this instanceof VegInfoView)
    {
        UI.Base.call(this);
        this.dom.style.position = "relative";
        var textP = document.createElement('p');
        textP.style.marginLeft = 'auto';
        textP.style.marginRight = 'auto';
        textP.style.width = '90%';
        textP.appendChild(document.createTextNode("vegname = " + veg.name + ", seed price = " + veg.seedPrice + ", sell price = " + veg.price + 
            ", season num = " + veg.numSeason + ", stage num = " + veg.numStage + ", mature veg num = " + veg.numMature + 
            ", got exp when harvest = " + veg.harvestExp + ", need level = " + veg.levelLimit + ", first time mature time = " + veg.firstMatureTime +
            ", other time mature time = " + veg.otherMatureTime));
        this.dom.appendChild(textP);
        // programing add form.
        var newDom = document.createElement('div');
        if (buy)
        {
            newDom.innerHTML = "<label>buy num</label><input type=\"text\" value=\"\">";
        }
        else
        {
            newDom.innerHTML = "<label>sell num</label><input type=\"text\" value=\"\">";
        }
        this.dom.appendChild(newDom);
        this.vegName = name;
    }
    else
    {
        return new VegInfoView(veg);
    }
}
inheritPrototype(VegInfoView, UI.Base);

VegInfoView.prototype.getInputVegNum = function()
{
    // get num from input and return. if no num inputed. return -1.
    var str = this.dom.getElementsByTagName("input")[0].value;
    log.info(str);
    var filter= /^\d+$/;
    if (!filter.test(str))
    {
        return -1;
    }
    return parseInt(str);
}

function VegBuyDialog(vegName)
{
    if (this instanceof VegBuyDialog)
    {
        UI.Dialog.call(this);
        this.dom.style.position = "relative";
        this.setClassName("ui_veg_buy_dialog");
        this.vegName = vegName;
        this.veg = StaticModels.getVegStore()[vegName];
        this.leftButtonName = "Buy";
        this.rightButtonName = "Cancel";
        this.leftCb = bind(this.onBuy, this);
    }
    else
    {
        return new VegBuyDialog(vegName);
    }
}
inheritPrototype(VegBuyDialog, UI.Dialog);

VegBuyDialog.prototype.onBuy = function()
{
    var buyNum = this.vegView ? this.vegView.getInputVegNum() : -1;
    if (buyNum <= 0)
    {
        var msg = new UI.SimpleDialogBox("You Input invalid num");
        msg.doModal();
        return false;
    }
    
    var veg = StaticModels.getVegInfo(this.vegName);
    var needMoney = buyNum * veg.seedPrice;
    var handler = function(dialog, name, num)
    {
        UI.getManager().endModelDialog(dialog);
        Models.getModels().buySeed(name, num);
        return true;
    }
    var msg = new UI.SimpleDialogBox("are you sure to buy " + buyNum + " "+ this.vegName + " with " + needMoney + " money?", "OK", curry(handler, this, this.vegName, buyNum), "Cancel");
    msg.doModal("OK");
    
    return false;
}

VegBuyDialog.prototype.fillBugContent = function()
{
    var veg = StaticModels.getVegInfo(this.vegName);
    this.vegView = new VegInfoView(veg, true);
    this.content.appendChild(this.vegView);
}

VegBuyDialog.prototype.render = function()
{
    // TODO dont know why top no margin. 
    this.dom.style.backgroundColor = 'white';
    this.dom.style.width = '350px';
    this.content = new UI.Base();
    this.content.dom.style.position = "relative";
    this.content.dom.style.marginLeft = "20px";
    this.content.dom.style.marginRight = "20px";
    this.content.dom.style.marginBottom = "10px";
    this.content.dom.style.marginTop = "20px";
    this.content.dom.style.backgroundColor = 'yellow';
    this.appendChild(this.content);
    
    this.fillBugContent();
    
    this.renderDefaultButtons();
}

// Warehouse + sell Dialog serious.

function WarehouseItemIcon(region, data)
{
    if (this instanceof WarehouseItemIcon)
    {
        UI.Base.call(this);
        this.resize(region);
        this.enableRawEvent(UI.Base.MOUSE_CLICK);
        this.registerListener(UI.Base.MouseEvent.CLICK, bind(this.onClick, this));
        var textP = document.createElement('p');
        textP.style.marginLeft = 'auto';
        textP.style.marginRight = 'auto';
        textP.style.width = '90%';
        textP.appendChild(document.createTextNode("vegname = " + data.name + " has num = " + data.num));
        this.dom.appendChild(textP);
        this.enableTextTip(data.name + " click to open sell dialog");
        this.vegName = data.name;
        this.vegNum = data.num;
    }
    else
    {
        return new WarehouseItemIcon(region, data);
    }
}
inheritPrototype(WarehouseItemIcon, UI.Base);

WarehouseItemIcon.prototype.onClick = function(e)
{
    log.info("click WarehouseItemIcon, " + this.vegName);
    var dialog = new VegSellDialog(this.vegName);
    dialog.doModal();
}

function VegSellDialog(vegName)
{
    if (this instanceof VegSellDialog)
    {
        UI.Dialog.call(this);
        this.dom.style.position = "relative";
        this.setClassName("ui_veg_sell_dialog");
        this.vegName = vegName;
        this.veg = StaticModels.getVegStore()[vegName];
        this.leftButtonName = "Sell";
        this.rightButtonName = "Cancel";
        this.leftCb = bind(this.onSell, this);

        this.vegView = null;
    }
    else
    {
        return new VegSellDialog(vegName);
    }
}
inheritPrototype(VegSellDialog, UI.Dialog);

VegSellDialog.prototype.onSell = function()
{
    var sellNum = this.vegView ? this.vegView.getInputVegNum() : -1;
    if (sellNum <= 0)
    {
        var msg = new UI.SimpleDialogBox("You Input invalid num");
        msg.doModal();
        return false;
    }
    
    var veg = StaticModels.getVegInfo(this.vegName);
    var needMoney = sellNum * veg.price;
    var handler = function(dialog, name, num)
    {
        UI.getManager().endModelDialog(dialog);
        Models.getModels().sellVeg(name, num);
        return true;
    }
    var msg = new UI.SimpleDialogBox("are you sure to sell " + sellNum + " "+ this.vegName + " with " + needMoney + " money?", "OK", curry(handler, this, this.vegName, sellNum), "Cancel");
    msg.doModal("OK");
    
    return false;
}

VegSellDialog.prototype.fillBugContent = function()
{
    var veg = StaticModels.getVegInfo(this.vegName);
    this.vegView = new VegInfoView(veg, false);
    this.content.appendChild(this.vegView);
}

VegSellDialog.prototype.render = function()
{
    // TODO dont know why top no margin. 
    this.dom.style.backgroundColor = 'white';
    this.dom.style.width = '350px';
    this.content = new UI.Base();
    this.content.dom.style.position = "relative";
    this.content.dom.style.marginLeft = "20px";
    this.content.dom.style.marginRight = "20px";
    this.content.dom.style.marginBottom = "10px";
    this.content.dom.style.marginTop = "20px";
    this.content.dom.style.backgroundColor = 'yellow';
    this.appendChild(this.content);
    
    this.fillBugContent();
    
    this.renderDefaultButtons();
}

function Warehouse()
{
    if (this instanceof Warehouse)
    {
        UI.Dialog.call(this);
        this.setClassName("ui_warehouse");
        this.leftButtonName = "OK";
        this.content = null;
        this.renderBasic();
        Models.getWarehouseItemList(bind(this.onReceiveWarehouseItem, this));
    }
    else
    {
        return new Warehouse();
    }
}
inheritPrototype(Warehouse, UI.Dialog);

Warehouse.prototype.render = function()
{
    
}

Warehouse.prototype.renderBasic = function()
{
    this.dom.style.backgroundColor = 'white';
    this.resize(new Rect(0, 0, 440, 470));
    this.content = new UI.Base();
    this.content.dom.style.position = "relative";
    this.content.dom.style.marginLeft = "20px";
    this.content.dom.style.marginRight = "20px";
    this.content.dom.style.marginBottom = "10px";
    this.content.dom.style.marginTop = "20px";
    this.content.setSize(new Point(400, 400));
    this.content.dom.style.overflowY = 'scroll';
    this.content.dom.style.overflowX = 'hidden';
    this.content.dom.style.backgroundColor = 'blue';
    this.appendChild(this.content);
    this.renderDefaultButtons();
}

Warehouse.prototype.getSize = function()
{
    return new Point(440, 470);
}

Warehouse.prototype.onReceiveWarehouseItem = function(vegs)
{
    log.info("warehouse item received!");
    var _ret = [];
    var i = 0;
    for (var k in vegs)
    {
        assert(vegs[k] > 0, "Models.getWarehouseItemList, every item must have > 0 nums");
        var row = Math.floor(i / 4);
        var col = i % 4;
        this.content.appendChild(new WarehouseItemIcon(
            new Rect(col * 100, row * 100, 100, 100), {'name' : k, 'num' : vegs[k]}));
    }
}

